#!/bin/sh
#
# Usage:
#   ./0-setup.sh <function name>

set -o nounset

# Why does /bin/sh require $PWD here?
. $PWD/util.sh

# Make user and group.  Needs to be run as root.
_make_user() {
  groupadd lfs
  useradd -s /bin/bash -g lfs -m -k /dev/null lfs
}

make_user() {
  sudo $0 _make_user
}

set_passwd() {
  sudo passwd lfs
}

# set up a clean environment for the lfs user.
# TODO: When it's fully-automated, we should just clear the environment at the
# beginning.  This is part of the cage definition.

set_bash() {
  cat >/home/lfs/.bash_profile <<EOF
exec env -i HOME=/home/lfs TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

  # TODO: Remove LFS?  Everything is automated, so we shouldn't need anything
  # special in the environment.  

  cat >/home/lfs/.bashrc <<EOF
set +h
umask 022
LFS=/home/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF
}

login() {
  su - lfs
}

make_dirs() {
  if test $(whoami) != lfs; then
    die "Must be the 'lfs' user."
  fi
  mkdir ~/tools ~/sources
  ls -al ~

}

# Why does LFS want this rather than just putting stuff in /tools?
# This is the ./configure prefix.
make_tools_link() {
  sudo ln -sv /home/lfs/tools /
}

"$@"
